<template>
  <!-- 实时还款查询 -->
  <section class="page repayment-record-query">
    <data-form :model="searchModel" @onSearch="refreshData" @handleResetForm='handleResetForm' :page="pageService" >
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="searchModel.caseNumber" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="组织机构" prop="deptCodes">
          <!-- <el-cascader v-model="searchModel.deptCodes" :options="departmentData" :props="{value:'deptCode',label:'deptName'}" @change="changeDept" change-on-select :show-all-levels="false" clearable></el-cascader> -->
          <select-department :specialOptions="specialOptions" v-model="searchModel.deptCodes" @on-select-change="(id, code) => departemntChange(id, code)"></select-department>
        </el-form-item>
        <el-form-item label="当前催收员" prop="collectorId">
          <el-select v-model="searchModel.collectorId" placeholder="请选择" clearable>
            <el-option v-for="item in userDataSet" :key="item.id" :label="item.realName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件类型" prop="caseType">
          <el-input v-model="searchModel.caseType" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="searchModel.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="searchModel.personalName" placeholder="请输入" clearable></el-input>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" @onPageChange="queryRepaymentRecord" :page="pageService">
        <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
            <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="idCard" label="身份证号码" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="caseType" label="案件类型" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="sysCode" label="系统来源" :formatter="(row)=>$filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="collectorName" label="催收员" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="overdueAmount" label="欠款总金额" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
             <el-table-column prop="overdueAmount" label="应还款金额" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="repayAmount" label="实际还款金额" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>  
            <el-table-column prop="entryTime" label="还款时间" :formatter="(row) => $filter.dateTimeFormat(row.entryTime)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
            </el-table-column>          
        </template>
        <p slot="footer">{{`查询结果统计：当前还款总金额 ${$filter.toThousands(totalInfo.totalAmount)}元`}}</p>
    </data-box>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import { SortService } from "~/utils/sort.service";
import NumberRange from "~/components/common/number-range.vue";
import { SynthesizeQueryService } from "~/services/domain-service/synthesize-query.service";
import { namespace, Getter, Mutation } from "vuex-class";
import { UserService } from "~/services/management-service/user.service";
import SelectDepartment from "~/components/business-component/select-department.vue";
import { CommonService } from "~/utils/common.service";
import { DepartmentService } from "~/services/management-service/department.service";
import { OpenExecutionData } from "~/config/types";

const CollectionExecutionModule = namespace('collection-execution');

@Layout("workspace")
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    SelectDepartment
  }
})
export default class RepaymentRecordQuery extends Vue {

  @Dependencies(PageService) private pageService: PageService;

  @Dependencies(SortService) private sortService: SortService;
  @Dependencies(DepartmentService) private departmentService: DepartmentService;

  @Dependencies(SynthesizeQueryService)
  private synthesizeQueryService: SynthesizeQueryService;

  @Dependencies(UserService)
  private userService: UserService;

  @CollectionExecutionModule.Action private openCollectionExecution;

  @Getter
  private departmentData;

  @Getter
  private isSupperAdmin;

  private totalInfo:any ={};
  private specialOptions: any[] = []

  private searchModel: any = {
    deptCodes: "",
    deptCode: "",
    collectorId: "",
    caseType:"",
    sysCode:"",
    caseNumber:"",
    personalName: ""
  };

  private dataSet: any[] = [];

  private userDataSet = [];

  private userModel: any = {
    deptCode : ""
  };
  private queryTotalAmount(){
    this.synthesizeQueryService
      .getTotalRepayment(this.searchModel)
      .subscribe(data => (this.totalInfo = data)); 
  }

  private queryRepaymentRecord(){
    this.synthesizeQueryService
        .queryRepaymentRecord(this.searchModel, this.pageService)
        .subscribe(data => (this.dataSet = data));
  }
  private handleResetForm() {
    this.searchModel.deptCode = ''
  }
  private refreshData() {
   this.queryRepaymentRecord();
   this.queryTotalAmount();
  }

  //获取委外和电催部门数据
  private queryTelAndVisitDepartment() {
    this.departmentService.queryTelAndVisitDepartment().subscribe(
      data =>{ this.specialOptions = CommonService.generateTreeData(data, {
        keyName: 'id',
        parentKeyName: 'parentId'
      })
    })
  }
  private departemntChange(id,code) {
    if (!code) {
      this.userDataSet = []
    } else {
      this.searchModel.deptCode = code
      this.getAllUserByDeptCode(code)
    }
  }

  private mounted() {
    // if(!(this.departmentData[0].deptCode)){
    //     return;
    // }
    // this.searchModel.deptCodes.push(this.departmentData[0].deptCode);
    // this.getAllUserByDeptCode();
    this.refreshData();
    this.queryTelAndVisitDepartment()
  }

  private getAllUserByDeptCode(deptCode: string){
    //  if(this.searchModel.deptCodes.length === 0){
    //       return;
    //   }
    //  this.userModel.deptCode = this.searchModel.deptCodes[this.searchModel.deptCodes.length-1]
    //  this.userService
    //     .getAllUsers(deptCode, this.pageService)
    //     .subscribe(data => {
    //         this.userDataSet = data;
    //   });
    this.userService.getAllUsersList({deptCode}).subscribe(
      data => this.userDataSet = data
    )
  }
  // private changeDept(){
  //   this.getAllUserByDeptCode()
  // }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(
      new OpenExecutionData(
        row.caseId,
        row.caseNumber,
        row.departmentType,
        true,
        true
      )
    );
  }
}
</script>
<style lang="less">
</style>
